package com.swss.util;

import java.util.Map;


public class HqlUtil {
	/**
	 * 
	 * @param obj
	 * @param as
	 * @param isAnd
	 *            也可理解为：是否为WHERE后面 紧邻的条件 false 字符串开头不加 AND (紧邻) true 字符串开头加 AND
	 *            (动态的如果hql为空则不加AND) (非紧邻)
	 * @return
	 */
	public static String bean2HqlLike(Object obj, String as) {
		String hql = "";
		if (obj == null) {
			return hql;
		}
		Map<String, String> map = BeanUtils.bean2Map(obj);
		for (Map.Entry<String, String> entry : map.entrySet()) {
			if (!entry.getValue().equals("")) {
				if (entry.getKey().equals("deptNo")) {
					if (!entry.getValue().equals("00")) {
						hql += " AND " + as + "." + entry.getKey() + " LIKE '"
								+ entry.getValue() + "%'";
					}
				} else {
					hql += " AND " + as + "." + entry.getKey() + " LIKE '%"
							+ entry.getValue() + "%'";
				}
			}
		}
		return hql;
		// if (hql.equals("")) {
		// return hql;
		// }
		// if (isAnd) {
		// return hql;
		// } else {
		// return hql.substring(4);
		// }

	}
    /**
     * 范围条件查询处理
     * @param obj
     * @return
     */
	public static String rangeHql(Object obj,String as) {

		String hql = "";
		if (obj == null) {
			return hql;
		}
		Map<String, String> map = BeanUtils.bean2Map(obj);
		for (Map.Entry<String, String> entry : map.entrySet()) {
			if (!entry.getValue().equals("")) {
				if (entry.getKey().indexOf("Start") != -1) {
					hql += " AND "+as+"."+ entry.getKey().replaceAll("Start", "")
							+ ">='" + entry.getValue() + "'";
				} else if (entry.getKey().indexOf("End") != -1) {
					hql += " AND "+as+"."+ entry.getKey().replaceAll("End", "")
							+ "<='" + entry.getValue() + "'";
				}
			}
		}

		return hql;

		// StringBuffer hql = new StringBuffer();
		// hql.append("a.price>=" + astRange.getPriceStart() + " AND a.price<="
		// + astRange.getPriceEnd());
		// hql.append("a.amount>=" + astRange.getAmountStart() + " AND
		// a.amount<="
		// + astRange.getAmountEnd());
		//
		// hql.append("a.buyDate>=" + astRange.getBuyDateStart() + " AND
		// a.buyDate<="
		// + astRange.getBuyDateStart());
		// hql.append("a.recordDate>=" + astRange.getRecordDateStart() + " AND
		// a.recordDate<="
		// + astRange.getRecordDateEnd());
		// return hql.toString();
	}
}
